using System.Collections.Generic;
using UnityEngine;

/// <summary>
///     ���м���assets �ļ��� �࣬�����Ǹ�ÿ��UI�����ݴ洢�ʹ����ã������ϸ�����������������ֻ��Ҫ����̬���ݹ�������
///     �����������Ϊģ��daoģʽ�еĵײ�һ����ֻ�ṩ�ϲ��װ
/// </summary>
public class InventoryManager : SingletonMono<InventoryManager>
{
    [Header("MainMenu")] [SerializeField] private List<MainMenu> _mainMenuList = new();

    [SerializeField] private MainMenu _currentActiveMainMenu;
    [SerializeField] private int _currentActiveMenuIndex;

    [Header("Item")] [SerializeField] private Bag m_Bag;

    [SerializeField] private GameObject slotGrid;

    [SerializeField] private Item slotPrefab;
    //public Text itemInformation;

    [Header("Item Details")] [SerializeField]
    private ItemDetails _itemDetailsPrefab;

    [SerializeField] private GameObject _itemDetailsMenu;
    [SerializeField] private ItemDetails _currentActiveItemDetails;


    //Quest List
    [Header("Quest List")] [SerializeField]
    private QuestAcceptedList _questAcceptedList;

    [SerializeField] private GameObject _questListMenu;
    [SerializeField] private Questitem _questItemPrefab;


    [Header("ItemDetils List")] private readonly Dictionary<string, ItemDetails> _ID_Dict = new();


    private void Start()
    {
        MainMenuInit();
        RefreshItem();
        QuestDetailsLLoad();
    }

    private void Update()
    {
        //������ʵӦ���и��õĽ�������ģ� ֻ����ʱ����input system���Һ���Ϥ��
        //�����⣺���������״̬��û�д򿪵������Ҳ�ܴ���
        if (LightInputManager.Instance.StatusBarTopMoveRightInput)
        {
            if (_currentActiveMenuIndex < 5) _currentActiveMenuIndex++;
            else _currentActiveMenuIndex = 0;
            MainMenuSwitch();
        }
        else if (LightInputManager.Instance.StatusBarTopMoveLeftInput)
        {
            if (_currentActiveMenuIndex > 0) _currentActiveMenuIndex--;
            else _currentActiveMenuIndex = 5;
            MainMenuSwitch();
        }
    }

    private void MainMenuInit()
    {
        _mainMenuList.ForEach(m => { m.gameObject.SetActive(false); });
        //goods����list�е�λ���Ѿ�ȷ�����о�����������
        _mainMenuList[2].gameObject.SetActive(true);
        _currentActiveMainMenu = _mainMenuList[2];
        _currentActiveMenuIndex = 2;
    }

    public void MainMenuSwitch(string name)
    {
        for (var i = 0; i < _mainMenuList.Count; i++)
            if (name.Equals(_mainMenuList[i].GetName()))
            {
                _currentActiveMainMenu.gameObject.SetActive(false);
                _mainMenuList[i].gameObject.SetActive(true);
                _currentActiveMainMenu = _mainMenuList[i];
            }
    }

    public void MainMenuSwitch()
    {
        _currentActiveMainMenu.gameObject.SetActive(false);
        _mainMenuList[_currentActiveMenuIndex].gameObject.SetActive(true);
        _currentActiveMainMenu = _mainMenuList[_currentActiveMenuIndex];
    }

    public void CreateNewItem(MyItem it)
    {
        var newItem = Instantiate(slotPrefab, slotGrid.transform.position, Quaternion.identity);
        newItem.gameObject.transform.SetParent(slotGrid.transform);
        //newItem._item = it;
        //newItem._itemName.text = it._name;
        //newItem._itImage.sprite = it.image;
        //newItem._itStatus.text = it._itemStatus;
        ItemDetailsDataLoad(it);
    }

    public void RefreshItem()
    {
        for (var i = 0; i < slotGrid.transform.childCount; i++)
        {
            if (slotGrid.transform.childCount == 0) return;
            //������ڿγ��н������ٺ�������ʵ��������Ϊֱ�������еĶ����н����޸��е��鷳��
            //����ʹ�ö���ش���
            Destroy(slotGrid.transform.GetChild(i).gameObject);
        }

        for (var i = 0; i < m_Bag.itemsList.Count; i++)
        {
            Debug.Log(m_Bag.itemsList[i].name);
            CreateNewItem(m_Bag.itemsList[i]);
        }
    }

    private void ItemDetailsDataLoad(MyItem it)
    {
        var _ID_it = Instantiate(_itemDetailsPrefab, _itemDetailsMenu.transform.position, Quaternion.identity);
        _ID_it.gameObject.transform.SetParent(_itemDetailsMenu.transform);
        _ID_it._ID_Name.text = it._name;
        _ID_it._ID_Damage.text = it.damage.ToString();
        //_ID_it._ID_Image.sprite = it.image;
        _ID_it._ID_Durability.text = it._durabiliy.ToString();
        _ID_it._ID_FixNum.text = it._fixNum.ToString();

        _ID_it.gameObject.SetActive(false);
        _ID_Dict.Add(it._name, _ID_it);
    }

    public void ItemDetailsMenuOpen(string name)
    {
        if (_ID_Dict.ContainsKey(name))
        {
            if (_currentActiveItemDetails != null) _currentActiveItemDetails.gameObject.SetActive(false);
            _currentActiveItemDetails = _ID_Dict[name];
            _currentActiveItemDetails.gameObject.SetActive(true);
        }
    }


    public void QuestBackendToDatabase(Quest quest)
    {
        _questAcceptedList._questList.Add(quest);
    }

    public void QuestDetailsLLoad()
    {
        for (var i = 0; i < _questAcceptedList._questList.Count; i++)
        {
            var newQuestItem = Instantiate(_questItemPrefab, _questListMenu.transform.position, Quaternion.identity);
            newQuestItem.gameObject.transform.SetParent(_questListMenu.transform);
            newQuestItem._itemName.text = _questAcceptedList._questList[i].questName;
            newQuestItem._itStatus.text = _questAcceptedList._questList[i].questStatus.ToString();
        }
    }
}